<?php

class GeneralController extends AbstractAdminController
{

    /**
     * Upload photos
     */
    public function actionUp()
    {
        $response = array('success' => false);

        if(empty($_FILES['files']) || empty($_FILES['files']['name']) || empty($_FILES['files']['name']['0']) || empty($_FILES['files']['tmp_name']['0']))
        {
            echo json_encode($response);
            Yii::app()->end();
        }

        $fileSrc = $_FILES['files']['tmp_name']['0'];
        $fileName = $_FILES['files']['name']['0'];
        $fileName = substr($fileName, 0, strrpos($fileName, '.'));
	    $targetType = $this->_getParam('target_type');

	    $aResize = !empty(Yii::app()->params->photos[$targetType]) ? Yii::app()->params->photos[$targetType] : array(
		    '-' => '1920/1920',
		    'list' => '200/200',
	    );

	    if($this->_getParam('disable_resize') > 0)
	    {
		    $aResize = array(
			    '-' => '1920/1920',
			    'list' => '200/200',
		    );
	    }

        $oThumb = new Thumbnail($fileSrc);
        $thumb = $oThumb->crop(
	        '/-/',
	        $aResize,
	        $fileName
        );

        if($thumb)
        {
            $oPhoto = new Photos();
            $oPhoto->attributes = array(
                'target_type' => '',
                'target_id' => '',
                'path' => $thumb['dir'],
                'file' => $thumb['file'],
                'upload_user' => 1,
                'upload_time' => time(),
                'is_main' => 0,
                'original_url' => ''
            );
            $oPhoto->save(false);
            $id = $oPhoto->getPrimaryKey();

            $response['id'] = $id;
            $response['file'] = $thumb['file'];
            $response['path'] = $thumb['dir'];
            $response['photos'] = array();
            foreach(Yii::app()->params->photos['goods'] as $key => $size)
            {
                $file = $thumb['dir'] . ($key == '-' ? '' : $key .'-') . $thumb['file'];

                if(is_readable(PUBLIC_PATH . $file))
                {
                    list($width, $height) = getimagesize(PUBLIC_PATH . $file);

                    $response['photos'][$key] = array(
                        'width' => $width,
                        'height' => $height,
                        'file' => $file
                    );
                }
            }

            $response['success'] = true;
            echo json_encode($response);

            Yii::app()->end();
        }


        echo json_encode($response);

        Yii::app()->end();
    }

    public function actionDeletePhoto($id)
    {
        $oPhoto = new Photos();
        $oFiles = new Files();

        $oPhoto = $oPhoto->findByPk($id);

        if($oPhoto)
        {
            $oFiles->deleteDirectory(PUBLIC_PATH . $oPhoto->path);

            $oPhoto->delete();

            echo json_encode(array('success' => true));
        }
        else
        {
            echo json_encode(array('success' => false));
        }

        Yii::app()->end();
    }

    public function actionDeleteVideo($id)
    {
        $oVideo = new Video();

        $oPhoto = $oVideo->findByPk($id);

        if($oPhoto)
        {
            $oPhoto->delete();

            echo json_encode(array('success' => true));
        }
        else
        {
            echo json_encode(array('success' => false));
        }

        Yii::app()->end();
    }

}